// API 模拟
const API = {
    // 基础配置
    baseURL: 'https://jsonplaceholder.typicode.com',
    
    // 模拟数据
    mockData: {
        posts: [
            {
                id: 1,
                author: '张三',
                avatar: 'assets/images/avatar.jpg',
                content: '今天天气真好，出去走走，感受春天的气息！🌸',
                images: ['assets/images/default/post1.jpg', 'assets/images/default/post2.jpg'],
                likes: 128,
                comments: 23,
                shares: 5,
                timestamp: Date.now() - 3600000,
                liked: false
            },
            {
                id: 2,
                author: '张三',
                avatar: 'assets/images/avatar.jpg',
                content: '分享一个超棒的前端学习资源，对新手特别友好！大家一起进步💪',
                images: [],
                likes: 256,
                comments: 45,
                shares: 12,
                timestamp: Date.now() - 7200000,
                liked: true
            },
            {
                id: 3,
                author: '张三',
                avatar: 'assets/images/avatar.jpg',
                content: '周末加班写代码，虽然累但是很有成就感。完成了一个复杂的功能模块！',
                images: ['assets/images/default/post3.jpg'],
                likes: 89,
                comments: 15,
                shares: 3,
                timestamp: Date.now() - 86400000,
                liked: false
            }
        ],
        
        albums: [
            {
                id: 1,
                name: '旅行相册',
                cover: 'assets/images/default/album1.jpg',
                count: 128,
                description: '记录走过的每一个地方'
            },
            {
                id: 2,
                name: '美食记录',
                cover: 'assets/images/default/album2.jpg',
                count: 86,
                description: '品尝各地的美食'
            },
            {
                id: 3,
                name: '生活点滴',
                cover: 'assets/images/default/album3.jpg',
                count: 234,
                description: '日常生活的美好瞬间'
            }
        ],
        
        videos: [
            {
                id: 1,
                title: '前端开发技巧分享',
                thumbnail: 'assets/images/default/video1.jpg',
                duration: '10:23',
                views: 1234,
                url: 'assets/videos/sample.mp4'
            },
            {
                id: 2,
                title: 'VLOG：周末的一天',
                thumbnail: 'assets/images/default/video2.jpg',
                duration: '05:45',
                views: 856,
                url: 'assets/videos/sample.mp4'
            },
            {
                id: 3,
                title: '编程学习心得',
                thumbnail: 'assets/images/default/video3.jpg',
                duration: '15:30',
                views: 2341,
                url: 'assets/videos/sample.mp4'
            }
        ],
        
        blogs: [
            {
                id: 1,
                title: '深入理解JavaScript闭包',
                excerpt: '闭包是JavaScript中的一个重要概念，理解闭包对于编写高质量的JavaScript代码至关重要...',
                views: 1523,
                comments: 45,
                timestamp: Date.now() - 172800000
            },
            {
                id: 2,
                title: 'Vue3 Composition API实践',
                excerpt: 'Vue3引入的Composition API为我们提供了更灵活的组件逻辑组织方式...',
                views: 892,
                comments: 23,
                timestamp: Date.now() - 259200000
            },
            {
                id: 3,
                title: '前端性能优化指南',
                excerpt: '性能优化是前端开发中永恒的话题，本文将分享一些实用的性能优化技巧...',
                views: 2103,
                comments: 67,
                timestamp: Date.now() - 345600000
            }
        ],
        
        visitors: [
            { id: 1, name: '李四', avatar: 'assets/images/default/visitor1.jpg' },
            { id: 2, name: '王五', avatar: 'assets/images/default/visitor2.jpg' },
            { id: 3, name: '赵六', avatar: 'assets/images/default/visitor3.jpg' },
            { id: 4, name: '钱七', avatar: 'assets/images/default/visitor4.jpg' },
            { id: 5, name: '孙八', avatar: 'assets/images/default/visitor5.jpg' },
            { id: 6, name: '周九', avatar: 'assets/images/default/visitor6.jpg' },
            { id: 7, name: '吴十', avatar: 'assets/images/default/visitor7.jpg' },
            { id: 8, name: '郑十一', avatar: 'assets/images/default/visitor8.jpg' }
        ]
    },

    // 获取动态列表
    async getPosts(page = 1, limit = 5) {
        return new Promise((resolve) => {
            setTimeout(() => {
                const start = (page - 1) * limit;
                const end = start + limit;
                const posts = this.mockData.posts.slice(start, end);
                resolve({
                    data: posts,
                    total: this.mockData.posts.length,
                    page,
                    limit
                });
            }, 500);
        });
    },

    // 发布动态
    async createPost(content, images = []) {
        return new Promise((resolve) => {
            setTimeout(() => {
                const newPost = {
                    id: Utils.generateId(),
                    author: '张三',
                    avatar: 'assets/images/avatar.jpg',
                    content,
                    images,
                    likes: 0,
                    comments: 0,
                    shares: 0,
                    timestamp: Date.now(),
                    liked: false
                };
                this.mockData.posts.unshift(newPost);
                resolve(newPost);
            }, 300);
        });
    },

    // 点赞动态
    async likePost(postId) {
        return new Promise((resolve) => {
            setTimeout(() => {
                const post = this.mockData.posts.find(p => p.id === postId);
                if (post) {
                    post.liked = !post.liked;
                    post.likes += post.liked ? 1 : -1;
                    resolve(post);
                }
            }, 200);
        });
    },

    // 获取相册列表
    async getAlbums() {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve(this.mockData.albums);
            }, 300);
        });
    },

    // 获取视频列表
    async getVideos() {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve(this.mockData.videos);
            }, 300);
        });
    },

    // 获取日志列表
    async getBlogs() {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve(this.mockData.blogs);
            }, 300);
        });
    },

    // 获取访客列表
    async getVisitors() {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve(this.mockData.visitors);
            }, 300);
        });
    },

    // 搜索功能
    async search(query, type = 'all') {
        return new Promise((resolve) => {
            setTimeout(() => {
                const results = {
                    posts: this.mockData.posts.filter(p => 
                        p.content.includes(query)
                    ),
                    albums: this.mockData.albums.filter(a => 
                        a.name.includes(query) || a.description.includes(query)
                    ),
                    videos: this.mockData.videos.filter(v => 
                        v.title.includes(query)
                    ),
                    blogs: this.mockData.blogs.filter(b => 
                        b.title.includes(query) || b.excerpt.includes(query)
                    )
                };
                resolve(type === 'all' ? results : results[type]);
            }, 400);
        });
    }
};

// 导出API
window.API = API;
